import time
import os
import urllib.request

import requests


def comp(ids):
    news_ids = []
    for id in ids:
        if id not in news_ids:
            news_ids.append(id)
    # print(news_ids)
    return news_ids


def getTimeName():
    t = time.strftime("%Y%m%d%H%M%S", time.localtime())
    # print("本地时间为 :", t)
    return t


def mkdir(path):
    folder = os.path.exists(path)
    if not folder:
        os.makedirs(path)
    else:
        print("---  There is this folder!  ---")


def downLoad(imageURL, fileName):
    if str(imageURL).__len__() < 5:
        return
    r = requests.get(imageURL)
    with open(fileName, 'wb') as outfile:
        outfile.write(r.content)


def GBK(url):
    return getHtmlOra(url, 'GBK')


def getHtmlUtf_8(url):
    return getHtmlOra(url, 'UTF-8')


MAC_HEADER = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) ' \
             'Chrome/71.0.3559.6 Safari/537.36 '
WIN_HEADER = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'


def getHtmlOra(url, format):
    headers = {'User-Agent': MAC_HEADER}
    # print(url)
    req = urllib.request.Request(url=url, headers=headers)
    data = urllib.request.urlopen(req).read()
    data = data.decode(format)
    # print(data)
    return data


def saveVideo(url,path):
    mkdir(path)
    if str(url) == None:
        return
    print("save_url = "+ url)
    splitPath = url.split('.')
    fTail = splitPath.pop()
    if len(fTail) > 3:
        fTail = 'mp4'
    fileName = path + "/" + getTimeName() + "." + fTail
    try:
        downLoad(url, fileName)
    except urllib.request.URLError as e:
        print(e.reason)


#根据视频列表保存视频
def saveVideoList(videoList,path):
    for imageURL in videoList:
        saveVideo(imageURL,path)


# 根据图片列表保存图片
def saveImages(imglist, path):
    mkdir(path)
    number = 1
    for imageURL in imglist:
        splitPath = imageURL.split('.')
        print(imageURL)
        fTail = splitPath.pop()
        if len(fTail) > 3:
            fTail = 'jpg'
        fileName = path + "/" + getTimeName() + "." + fTail
        print(fileName)
        # 对于每张图片地址，进行保存
        try:
            downLoad(imageURL, fileName)
        except urllib.request.URLError as e:
            print(e.reason)
        number += 1
